package com.jieshuibao.util.push;


import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.jieshuibao.model.domain.HttpResult;
import com.jieshuibao.service.HttpClientService;
@Component
public class PushClient {
	
	// The user agent
	protected final String USER_AGENT = "Mozilla/5.0";

	// This object is used for sending the post request to Umeng
	
	// The host
	protected static final String host = "http://msg.umeng.com";
	
	// The upload path
	protected static final String uploadPath = "/upload";
	
	// The post path
	protected static final String postPath = "/api/send";
	
	//private static ApplicationContext ctx = SpringUtils.getApplicationContext();
	
	private static final Logger logger = Logger.getLogger(PushClient.class);
	
	@Resource
	private HttpClientService httpClientService;
	//过滤
	public boolean sendTwo(UmengNotification msg) throws Exception {
		//HttpClientService httpClient = (HttpClientService) ctx.getBean("httpClientService");
				
		String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));
		msg.setPredefinedKeyValue("timestamp", timestamp);
        String url = host + postPath;
        String postBody = msg.getPostBody();
        String sign = DigestUtils.md5Hex(("POST" + url + postBody + msg.getAppMasterSecret()).getBytes("utf8"));
        url = url + "?sign=" + sign;
       System.out.println(msg.getPostBody());
        HttpResult httpResult = httpClientService.doPostJson(url, msg.getPostBody());
        
        System.out.println("test"+httpResult.getStatus()+httpResult.getData());
        logger.info("推送消息"+httpResult.getStatus()+httpResult.getData());
        return true;
    }
	public boolean sendThree(String msg,String appMasterSercret) throws Exception {
		//HttpClientService httpClient = (HttpClientService) ctx.getBean("httpClientService");
				
		
        String url = host + postPath;
      
        String sign = DigestUtils.md5Hex(("POST" + url + msg + appMasterSercret).getBytes("utf8"));
        url = url + "?sign=" + sign;
       
        HttpResult httpResult = httpClientService.doPostJson(url, msg);
        
        System.out.println("test"+httpResult.getStatus()+httpResult.getData());
        
        return true;
    }

}
